package com.sibbay.tools;

import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;

import com.sibbay.tools.java.util.Arrays;

import android.net.Uri;

public class Tools {

	// return 00:00A format
	public static String getCurrentTime() {
		final GregorianCalendar cal = new GregorianCalendar();
		return String.format("%02d:%02d%s", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.AM_PM) == Calendar.AM? "A": "P");
	}
	
//	// compare time in format 00:00A   
//	public static boolean biggerOrEqual(final String time1, final String time2) {
//		final boolean t12_1 = time1.substring(0, 2).equals("12");
//		final boolean t12_2 = time2.substring(0, 2).equals("12");
//		char ap1 = time1.charAt(5);
//		char ap2 = time2.charAt(5);
//		if (t12_1 || t12_2) {	// special case
//			ap1 = ap1 == 'A'? (t12_1? 'P': 'A'): (t12_1? 'A': 'P');
//			ap2 = ap2 == 'A'? (t12_2? 'P': 'A'): (t12_2? 'A': 'P');
//		}
//		if (ap1 > ap2)
//			return true;
//		
//		if (ap1 < ap2)
//			return false;
//		
//		return time1.compareTo(time2) >= 0;
//	}

//	// return time1 - time2
//	static boolean diffTime(final String time1, final String time2) {
//		final boolean t12_1 = time1.substring(0, 2).equals("12");
//		final boolean t12_2 = time2.substring(0, 2).equals("12");
//		char ap1 = time1.charAt(5);
//		char ap2 = time2.charAt(5);
//		if (t12_1 || t12_2) {	// special case
//			ap1 = ap1 == 'A'? (t12_1? 'P': 'A'): (t12_1? 'A': 'P');
//			ap2 = ap2 == 'A'? (t12_2? 'P': 'A'): (t12_2? 'A': 'P');
//		}
//		if (ap1 > ap2)
//			return true;
//		
//		if (ap1 < ap2)
//			return false;
//		
//		return time1.compareTo(time2) >= 0;
//	}

	// convert time from format 00:00A to integer   
	public static int convertToInt(final String time) {
		final int h = Integer.parseInt(time.substring(0, 2));
		final int m = Integer.parseInt(time.substring(3, 5));
		final int a;
		if (h == 12) {
			a = time.codePointAt(5) == 'A'? 12: 0;
		} else {
			a = time.codePointAt(5) == 'A'? 0: 12;
		}
		return (h + a) * 60 + m;
	}
	
	public static String convertToStringDiff(int diff) {
		if (diff < 60) {
			return String.format("%2dm", diff);
		}
		return String.format("%dh %2dm", diff/60, diff%60);
	}
	
	public static String getKey(final String uri) {
		return Uri.parse(uri).getLastPathSegment();
	}

	// search
	
    private static final Comparator<String> sTimeComparator = new Comparator<String>() {
		@Override
		public int compare(String time1, String time2) {
			return convertToInt(time1) - convertToInt(time2);
		}
	};

	// return: position in array
	//  0 - insert before 0
	// -1 - insert after last element
    public static int binSearch(final String[] array, int startIndex, int endIndex, final String time) {
    	int result = Arrays.binarySearch(array, startIndex, endIndex, time, sTimeComparator);
    	result = result > 0? result: -result - 1;
    	if (result < endIndex)
    		return result;
    	
    	if (convertToInt(time) <= convertToInt(array[startIndex]))
    		return startIndex;
    	
    	return -1;
    }
}
